/*
 * @(#) SimulatePanel.java 2013-7-19 上午10:58:58
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.base.control;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * 数字输入器
 * @author Nemo, 2013-9-9 下午1:48:57
 */
public class NumberInputPanel extends JFrame implements ActionListener
{
	private JPanel jPanel1, jPanel2;

	private JTextField resultField;

	private JButton s1, s2, s3, s4, s5, s6, s7, s8, s9, s0, f1, d1, d2, d3, d4;

	private boolean end;

	private String str;

	private String value;

	public NumberInputPanel()
	{
		super("计算器");
		setSize(
			400, 300);
		Container con = getContentPane();
		con.setLayout(new BorderLayout());
		jPanel1 = new JPanel();
		jPanel1.setLayout(new GridLayout(1, 1));
		jPanel2 = new JPanel();
		jPanel2.setLayout(new GridLayout(3, 5));
		resultField = new JTextField("0");
		jPanel1.add(resultField);
		con.add(
			jPanel1, BorderLayout.NORTH);
		s1 = new JButton("  1  ");
		s1.addActionListener(this);
		s2 = new JButton("  2  ");
		s2.addActionListener(this);
		s3 = new JButton("  3  ");
		s3.addActionListener(this);
		s4 = new JButton("  4  ");
		s4.addActionListener(this);
		s5 = new JButton("  5  ");
		s5.addActionListener(this);
		s6 = new JButton("  6  ");
		s6.addActionListener(this);
		s7 = new JButton("  7  ");
		s7.addActionListener(this);
		s8 = new JButton("  8  ");
		s8.addActionListener(this);
		s9 = new JButton("  9  ");
		s9.addActionListener(this);
		s0 = new JButton("  0  ");
		s0.addActionListener(this);
		f1 = new JButton("  .  ");
		f1.addActionListener(this);
		d1 = new JButton("  -  ");
		d1.addActionListener(this);
		d2 = new JButton(" CE ");
		d2.addActionListener(this);
		d3 = new JButton(" OK ");
		d3.addActionListener(this);
		d4 = new JButton("CANCEL");
		d4.addActionListener(this);
		jPanel2.add(s1);
		jPanel2.add(s2);
		jPanel2.add(s3);
		jPanel2.add(s4);
		jPanel2.add(s5);
		jPanel2.add(s6);
		jPanel2.add(s7);
		jPanel2.add(s8);
		jPanel2.add(s9);
		jPanel2.add(s0);
		jPanel2.add(f1);
		jPanel2.add(d1);
		jPanel2.add(d2);
		jPanel2.add(d3);
		jPanel2.add(d4);
		con.add(
			jPanel2, BorderLayout.CENTER);

	}

	NumberEditor numberEditor = null;
	public NumberInputPanel(NumberEditor numberEditor)
	{
		this();
		this.numberEditor = numberEditor;
	}

	public void num(int i)
	{
		String s = null;
		s = String.valueOf(i);
		if (end)
		{
			// 如果数字输入结束，则将文本框置零，重新输入
			resultField.setText("0");
			end = false;

		}
		if ((resultField.getText()).equals("0") || (resultField.getText()).equals("-0"))
		{
			if(resultField.getText().equals("-0"))
			{
				s = "-"+s;
			}
			// 如果文本框的内容为零，则覆盖文本框的内容
			resultField.setText(s);

		}
		else
		{
			// 如果文本框的内容不为零，则在内容后面添加数字
			str = resultField.getText() + s;
			resultField.setText(str);

		}
	}

	public void actionPerformed(ActionEvent e)
	{ // 数字事件
		if (e.getSource() == s1)
			num(1);
		else if (e.getSource() == s2)
			num(2);
		else if (e.getSource() == s3)
			num(3);
		else if (e.getSource() == s4)
			num(4);
		else if (e.getSource() == s5)
			num(5);
		else if (e.getSource() == s6)
			num(6);
		else if (e.getSource() == s7)
			num(7);
		else if (e.getSource() == s8)
			num(8);
		else if (e.getSource() == s9)
			num(9);
		else if (e.getSource() == s0)
			num(0);

		//功能事件
		else if (e.getSource() == d1)
			backText();
		else if (e.getSource() == d2)
			resultField.setText("0");
		else if (e.getSource() == d3)
			confirm(true);
		else if (e.getSource() == d4)
			confirm(false);
		
		// 等号
		else if (e.getSource() == f1)
		{
			str = resultField.getText();
			if (str.indexOf(".") <= 1)
			{
				str += ".";
				resultField.setText(str);
			}
		}
	}

	private void confirm(boolean isConfirm)
	{
		if(isConfirm)
		{
			Float value = null;
			try{
				value = Float.parseFloat(resultField.getText());
			}catch (Exception e) {
				value = null;
			}
			if(value != null)
			{
				numberEditor.setValue(value);
			}
		}
		this.setVisible(false);
	}

	private void backText()
	{
		String text = resultField.getText();
		if(text.startsWith("-"))
		{
			resultField.setText(text.substring(1, text.length()));
		}
		else
		{
			resultField.setText("-"+text);
		}
	}

	public String getValue()
	{
		return value;
	}

	public void setValue(String value)
	{
		this.value = value;
	}
	
	@Override
	public void setVisible(boolean b)
	{
		super.setVisible(b);
		this.value = null;
		resultField.setText("0");
	}
}
